package com.pxmeta.app.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pxmeta.common.core.redis.RedisCache;
import com.pxmeta.common.utils.StringUtils;
import com.pxmeta.dynamic.entity.Dynamic;
import com.pxmeta.dynamic.service.IDynamicService;
import com.pxmeta.dynamic.service.RedisLockService;
import com.pxmeta.party.entity.ActivityAdAstraRecord;
import com.pxmeta.party.entity.AssociationUser;
import com.pxmeta.party.service.IActivityAdAstraRecordService;
import com.pxmeta.party.service.IAssociationUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class AutoDelDynamicTask {

    private final IDynamicService dynamicService;
    private final IAssociationUserService associationUserService;
    private final IActivityAdAstraRecordService activityAdAstraRecordService;
    private final RedisCache redisCache;
    private final RedisLockService redisLockService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void autoDelDynamic(){
        String lock = redisLockService.lock("task_autoDelDynamic", 1000, 200);

        if (StringUtils.isNotEmpty(lock)){
            List<Dynamic> blockedList = dynamicService.getBlockedList();
            log.info("开始执行自动删除动态task,违规动态数：{}", blockedList.size());
            dynamicService.removeBatchByIds(blockedList);
            log.info("违规动态清除成功！");
        }
    }


    //每隔60秒，重置奖励
    @Scheduled(cron = "0/60 * * * * ?")
    public void autoUpdateAdAstraCountdownTime() throws InterruptedException {
        String lock = redisLockService.lock("task_autoUpdateAdAstraCountdownTime", 1000, 200);
        if (StringUtils.isNotBlank(lock)){
            // 设置倒计时结束时间为当前时间 + 30秒
            Long endTimeMillis = System.currentTimeMillis() + (60 * 1000);
            redisCache.setCacheObject("countdown_time",endTimeMillis);
            activityAdAstraRecordService.resetRecord();
        }
    }

    //每隔55秒，统计本次奖品盈亏
    @Scheduled(cron = "50 * * * * ?")
    public void computeAndResetAdAstraRecord(){
        String lock = redisLockService.lock("task_computeAndResetAdAstraRecord", 1000, 200);
        if (StringUtils.isNotBlank(lock)){
            activityAdAstraRecordService.computeRecord();
        }
    }


    @Scheduled(cron = "0 0 0 ? * MON")
    public void autoCleanWeekEarnings(){
        String lock = redisLockService.lock("task_autoCleanWeekEarnings", 1000, 200);
        if (StringUtils.isNotBlank(lock)){
            LambdaQueryWrapper<AssociationUser> wrapper = Wrappers.lambdaQuery();
            wrapper.ne(AssociationUser::getWeekEarnings,0);
            List<AssociationUser> list = associationUserService.list(wrapper);
            list.forEach(l->l.setWeekEarnings(BigDecimal.ZERO));
            log.info("开始执行自动清空主播周收益,成员数：{}",list.size());
            if (CollectionUtils.isNotEmpty(list)){
                associationUserService.updateBatchById(list);
            }
            log.info("周收益清空成功！");
        }
    }
}
